/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditProfile.java
 *
 * Created on Aug 31, 2011, 3:04:56 PM
 */
package Views;

import Controllers.AccountController;
import Controllers.PropertiesUtility;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import Models.Account;
import java.text.SimpleDateFormat;

/**
 *
 * @author c0910m1479
 */
public class EditProfile extends javax.swing.JDialog {

    File sourceImage = null;
    String[] user = PropertiesUtility.getUserProperties();
    Account profile;

    /** Creates new form EditProfile */
    public EditProfile(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        if (AccountController.login(user[0], user[1]) != null) {
            profile = AccountController.GetAccountByID(Integer.parseInt(user[2]));
            loadValues();
            this.setLocationRelativeTo(null);
        } else {
            new Login().setVisible(true);
            JOptionPane.showMessageDialog(this, "User.Properties had been modified !", "Error", JOptionPane.ERROR_MESSAGE);
            File userProp = new File(System.getProperty("user.dir") + "\\user.properties");
            userProp.delete();
            System.exit(0);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jbSave = new javax.swing.JButton();
        jtfPhone = new javax.swing.JTextField();
        jbReset = new javax.swing.JButton();
        jtfPicture = new javax.swing.JTextField();
        jbCancel = new javax.swing.JButton();
        jtfAddress = new javax.swing.JTextField();
        jbBrowse = new javax.swing.JButton();
        picture = new javax.swing.JLabel();
        password = new javax.swing.JLabel();
        caption = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jtfSerialNo = new javax.swing.JTextField();
        jtfName = new javax.swing.JTextField();
        jtfDOB = new javax.swing.JTextField();
        jtfRegion = new javax.swing.JTextField();
        jtfDep = new javax.swing.JTextField();
        jtfRank = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jtfRole = new javax.swing.JTextField();
        jcbRetired = new javax.swing.JCheckBox();
        jrbMale = new javax.swing.JRadioButton();
        jrbFemale = new javax.swing.JRadioButton();
        jtfPassword = new javax.swing.JPasswordField();
        profilePic = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jbSave.setText("Save");
        jbSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSaveActionPerformed(evt);
            }
        });

        jbReset.setText("Reset");
        jbReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbResetActionPerformed(evt);
            }
        });

        jbCancel.setText("Cancel");
        jbCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelActionPerformed(evt);
            }
        });

        jbBrowse.setText("Browse");
        jbBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBrowseActionPerformed(evt);
            }
        });

        picture.setFont(new java.awt.Font("Courier New", 0, 14));
        picture.setText("Picture:");

        password.setFont(new java.awt.Font("Courier New", 0, 14));
        password.setText("Password:");

        caption.setFont(new java.awt.Font("Courier New", 1, 18));
        caption.setText("PROFILE INFORMATION");

        jLabel1.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel1.setText("Serial Number:");

        jLabel2.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel2.setText("Full Name:");

        jLabel3.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel3.setText("Gender:");

        jLabel4.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel4.setText("Rank:");

        jLabel5.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel5.setText("Department:");

        jLabel6.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel6.setText("Region:");

        jLabel7.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel7.setText("Address:");

        jLabel8.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel8.setText("Date Of Birth (MM/dd/yyyy):");

        jLabel9.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel9.setText("Phone:");

        jLabel10.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel10.setText("Retired:");

        jtfSerialNo.setEditable(false);
        jtfSerialNo.setEnabled(false);

        jtfName.setEditable(false);
        jtfName.setEnabled(false);

        jtfDOB.setEditable(false);
        jtfDOB.setEnabled(false);

        jtfRegion.setEditable(false);
        jtfRegion.setEnabled(false);

        jtfDep.setEditable(false);
        jtfDep.setEnabled(false);

        jtfRank.setEditable(false);
        jtfRank.setEnabled(false);

        jLabel11.setFont(new java.awt.Font("Courier New", 0, 14));
        jLabel11.setText("System Role:");

        jtfRole.setEditable(false);
        jtfRole.setEnabled(false);

        jcbRetired.setEnabled(false);

        jrbMale.setText("Male");
        jrbMale.setEnabled(false);

        jrbFemale.setText("Female");
        jrbFemale.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(caption)
                        .addGap(345, 345, 345))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(52, 52, 52)
                                .addComponent(jtfAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(36, 36, 36)
                                .addComponent(jtfName, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                            .addComponent(jbReset)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(password)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel11))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jrbMale)
                                        .addGap(29, 29, 29)
                                        .addComponent(jrbFemale))
                                    .addComponent(jtfSerialNo, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                                    .addComponent(jtfRole)
                                    .addComponent(jtfPhone)
                                    .addComponent(jtfPassword))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4))
                                .addGap(132, 132, 132)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jtfPicture, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jbBrowse)
                                        .addGap(117, 117, 117))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addComponent(jtfRegion, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jtfDep, javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jtfRank, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE))
                                        .addContainerGap())))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(picture)
                                .addContainerGap())
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel10))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jtfDOB, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                                            .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jbSave)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                                                .addComponent(jbCancel)))
                                        .addGap(190, 190, 190))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jcbRetired)
                                        .addContainerGap())))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(caption)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jtfSerialNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfRank, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password)
                    .addComponent(jLabel5)
                    .addComponent(jtfDep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jtfRegion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jtfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtfAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(picture)
                    .addComponent(jtfPicture, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbBrowse))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel10)
                    .addComponent(jcbRetired)
                    .addComponent(jrbMale)
                    .addComponent(jrbFemale))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jtfPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jtfDOB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jtfRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbReset)
                    .addComponent(jbSave)
                    .addComponent(jbCancel))
                .addGap(23, 23, 23))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 764, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(profilePic)
                .addContainerGap(241, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(profilePic))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbResetActionPerformed
    jtfPassword.setText(null);
    jtfPicture.setText(null);
    jtfAddress.setText(null);
    jtfPhone.setText(null);
}//GEN-LAST:event_jbResetActionPerformed

private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_jbCancelActionPerformed

private void jbBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBrowseActionPerformed
    JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir") + "/images/accounts/");
    if (fileChooser.showDialog(this, "Select") == JFileChooser.APPROVE_OPTION) {
        sourceImage = fileChooser.getSelectedFile();
        jtfPicture.setText("images/accounts/" + sourceImage.getName());
        ImageIcon icon = new ImageIcon(sourceImage.getPath().replace('\\', '/'));
        profilePic.setIcon(resizeIcon(icon));
    }
}//GEN-LAST:event_jbBrowseActionPerformed

private void jbSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSaveActionPerformed
    if (isComponentValid()) {
        copyImage();
        profile.setPassword(new String(jtfPassword.getPassword()));
        profile.setPicture(jtfPicture.getText());
        profile.setAddress(jtfAddress.getText());
        profile.setPhoneNo(jtfPhone.getText());
        if (AccountController.EditAccount(profile)) {
            JOptionPane.showMessageDialog(this, "Profile Edited", "Success", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "Error", "Error", JOptionPane.ERROR_MESSAGE);
        }
        this.dispose();
    }
}//GEN-LAST:event_jbSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel caption;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbBrowse;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbReset;
    private javax.swing.JButton jbSave;
    private javax.swing.JCheckBox jcbRetired;
    private javax.swing.JRadioButton jrbFemale;
    private javax.swing.JRadioButton jrbMale;
    private javax.swing.JTextField jtfAddress;
    private javax.swing.JTextField jtfDOB;
    private javax.swing.JTextField jtfDep;
    private javax.swing.JTextField jtfName;
    private javax.swing.JPasswordField jtfPassword;
    private javax.swing.JTextField jtfPhone;
    private javax.swing.JTextField jtfPicture;
    private javax.swing.JTextField jtfRank;
    private javax.swing.JTextField jtfRegion;
    private javax.swing.JTextField jtfRole;
    private javax.swing.JTextField jtfSerialNo;
    private javax.swing.JLabel password;
    private javax.swing.JLabel picture;
    private javax.swing.JLabel profilePic;
    // End of variables declaration//GEN-END:variables

    private void loadValues() {
        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        jtfSerialNo.setText(profile.getSerialNo());
        jtfPassword.setText(profile.getPassword());
        jtfPicture.setText(profile.getPicture());
        jtfName.setText(profile.getFullName());
        jrbMale.setSelected(profile.isGender());
        jrbFemale.setSelected(!profile.isGender());
        jtfRank.setText(profile.getRank());
        jtfRole.setText(profile.getRole());
        jtfDep.setText(profile.getDepartment());
        jtfRegion.setText(profile.getRegion());
        jtfAddress.setText(profile.getAddress());
        jtfDOB.setText(formatter.format(profile.getDOB()));
        jtfPhone.setText(profile.getPhoneNo());
        jcbRetired.setSelected(profile.isRetired());
        profilePic.setIcon(resizeIcon(new ImageIcon(profile.getPicture())));
    }

    private boolean isComponentValid() {
        String pass = new String(jtfPassword.getPassword());
        String pic = jtfPicture.getText();
        String add = jtfAddress.getText();
        String num = jtfPhone.getText();
        if (pass == null || pass.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (pic == null || pic.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (add == null || add.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (num == null || num.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    private void copyImage() {
        File destDir = new File(System.getProperty("user.dir") + "/images/accounts/");
        File destImage = new File(System.getProperty("user.dir") + "/images/accounts/" + sourceImage.getName());

        if (!sourceImage.getPath().equals(destImage.getPath())) {
            try {
                if (!destDir.exists()) {
                    destDir.mkdirs();


                }
                FileChannel inChannel = new FileInputStream(sourceImage).getChannel();
                FileChannel outChannel = new FileOutputStream(destImage).getChannel();

                try {
                    if (!(inChannel.size() == inChannel.transferTo(0, inChannel.size(), outChannel))) {
                        JOptionPane.showMessageDialog(this, "Error Copying File", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(EditProfile.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (inChannel != null) {
                        inChannel.close();

                    }
                    if (outChannel != null) {
                        outChannel.close();

                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(EditProfile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private ImageIcon resizeIcon(ImageIcon icon) {
        ImageIcon resizedImageIcon = null;
        float iconRatio = (float) icon.getIconHeight() / (float) icon.getIconWidth();
        float fitToRatio = (float) 200 / (float) 200;
        if (iconRatio > fitToRatio) {
            resizedImageIcon = new ImageIcon(icon.getImage().getScaledInstance(-1, 200, java.awt.Image.SCALE_SMOOTH));
        } else {
            resizedImageIcon = new ImageIcon(icon.getImage().getScaledInstance(200, -1, java.awt.Image.SCALE_SMOOTH));
        }
        return resizedImageIcon;
    }
}
